import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';

import 'package:ymApp/router/router_handler.dart';

class Routes {
  static Router router = Router();
  static String index = '/index';
  static String channel = '/channel';
  static String profile = '/profile';
  static String indexDetail = '/index/detail';
  static String search = '/index/search';
  static String news = '/news';


  static void configureRoutes(Router router) {
    // 路由错误页面
    router.notFoundHandler = Handler(
        handlerFunc: (BuildContext context, Map<String, List<String>> params) {
         return Center(
           child: Text('路由未找到！！')
         );
    });

    router.define(index, handler: indexHandle);
    router.define(profile, handler: profileHandle);
    router.define(channel, handler: channelHandle);
    router.define(indexDetail, handler: indexDetailHandle);
    router.define(search, handler: searchHandle);
    router.define(news, handler: newsHandle);
  }

  // 对参数进行encode，解决参数中有特殊字符，影响fluro路由匹配(https://www.jianshu.com/p/e575787d173c)
  static Future navigateTo(BuildContext context, String path,
      { Map<String, dynamic> params,
      bool clearStack = false,
      TransitionType transition = TransitionType.fadeIn}) {
    String query = "";
    
    print(path);
    print(router);
    if (params != null) {
      int index = 0;
      for (var key in params.keys) {
        var value = Uri.encodeComponent(params[key]);
        if (index == 0) {
          query = "?";
        } else {
          query = query + "\&";
        }
        query += "$key=$value";
        index++;
      }
    }
    path = path + query;
    return router.navigateTo(context, path,
        clearStack: clearStack, transition: transition);
  }
}
